package panel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import file.DB;
import file.IOStreamStudent;
import listener.ScoreListener;
import model.Student;

public class ScoreTable extends JFrame implements ActionListener{
	
	public static ScoreTable instance=new ScoreTable();
	String column[]=new String[]{"学号","姓名","考勤","测验","作业","期末","总评"};
	public  DefaultTableModel model;
	public  JTable score=new JTable();
	public  JButton add=new JButton("添加");
	public  JButton delete=new JButton("删除");
	public  JButton edit=new JButton("修改");
	public  JButton quduan=new JButton("成绩区段");
	public  JScrollPane scroll=new JScrollPane(score);
	
	public ScoreTable(){
		this.setSize(800, 300);
		this.setTitle("查看学生成绩");
		this.setLayout(new BorderLayout());
		JPanel downPanel=new JPanel();
        downPanel.add(add);
        downPanel.add(delete);
        downPanel.add(edit);
        downPanel.add(quduan);
        this.add(downPanel,BorderLayout.SOUTH);
        ScoreListener listener=new ScoreListener();
      add.addActionListener(listener);
      delete.addActionListener(listener);
      edit.addActionListener(listener);
      quduan.addActionListener(listener);
		score = new JTable(); //创建一个表显示数据
        score.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        score.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        score.setRowHeight(30);
        JTableHeader header = score.getTableHeader(); // 获取 JTable 的头的对象
        header.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        header.setPreferredSize(new Dimension(header.getWidth(), 35)); // 设置此组件的首选大小

        model = (DefaultTableModel) score.getModel();
        model.setColumnIdentifiers(column);
   
        //Student stu=new Student("郑伟波", "201727010326", "5", "22", "18", "45", "90","面向对象程序设计","梁早清","2017级软件工程专业3班");
        //Student stu=new Student("", "", "", "", "", "", "","","","");
        //DB.arrStu.add(stu);
        //IOStreamStudent.writeToFile();
        ArrayList<Student> list=new ArrayList<>();
        //DB.arrStu.clear();
        //DB.arrStu=IOStreamStudent.readFromFile();
        list=IOStreamStudent.readFromFile();
        String[][] student=new String[list.size()][7];
        for(int i=0;i<list.size();i++){
        	for(int j=0;j<7;j++){
        	if(list.get(i).getCourse().equals(ScorePage.instance.box.getSelectedItem().toString())&&
        			list.get(i).getTeacher().equals(ScorePage.instance.box2.getSelectedItem().toString())&&
        			list.get(i).getStu_class().equals(ScorePage.instance.box3.getSelectedItem().toString())){
        		if(j==0)
        			student[i][j]=list.get(i).getXueHao();
        		if(j==1)
        			student[i][j]=list.get(i).getName();
        		if(j==2)
        			student[i][j]=list.get(i).getKaoQing();
        		if(j==3)
        			student[i][j]=list.get(i).getCeYan();
        		if(j==4)
        			student[i][j]=list.get(i).getZuoYe();
        		if(j==5)
        			student[i][j]=list.get(i).getQiMo();
        		if(j==6)
        			student[i][j]=list.get(i).getZongJi();
        	 	}
        	}
        }
        JTable table=new JTable(student, column);
//        TableModel tableModel = new DefaultTableModel(student, column);
//        JTable table = new JTable(tableModel);
     // 使用 表格模型 创建 行排序器（TableRowSorter 实现了 RowSorter）
        //RowSorter<TableModel> rowSorter = new TableRowSorter<TableModel>(tableModel);

        // 给 表格 设置 行排序器
       // table.setRowSorter(rowSorter);
        
        scroll.setViewportView(table);
        this.add(scroll);
        setVisible(true);
        this.setLocationRelativeTo(null);//位于屏幕中央
		this.setResizable(false);//设置此窗体是否可由用户调整大小
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置默认关闭操作
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new ScoreTable();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JButton button=(JButton) e.getSource();
		if(button==ScorePage.instance.update){
			new ScoreTable();
		}
	}

}
